package nl.harmidi.swing;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.util.prefs.Preferences;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import nl.harmidi.main.Application;

public class MidiOptionsPanel extends JPanel {

	private JSlider channelSlider;
	private Application app;
	
	public MidiOptionsPanel(Application app) {
		this.app = app;
		initGui();
	}
	
	private void initGui() {
		setLayout(new BorderLayout());
		createChannelSlider();
		add(channelSlider,BorderLayout.CENTER);
	}
		
	private void createChannelSlider() {
		
		int maxChannel = 16;
		
		channelSlider = new JSlider(JSlider.HORIZONTAL,1,maxChannel,app.getChannelToShow());

		channelSlider.setMajorTickSpacing(1);
		channelSlider.setMinorTickSpacing(0);
		channelSlider.setPaintTicks(true);
		channelSlider.setPaintLabels(true);
		channelSlider.setSnapToTicks(true);

		channelSlider.setToolTipText("Midi channel to show");
		
		int width = 500;
		try {
			int appWidth = Preferences.userRoot().getInt("frame.width",1000);
			width = appWidth - 300;
		} catch (Exception ignore) {}
		channelSlider.setBounds(new Rectangle(width,(int)channelSlider.getBounds().getHeight()));
		
		channelSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
			    JSlider source = (JSlider)e.getSource();
			    if (!source.getValueIsAdjusting()) {
			        int newChannel = (int)source.getValue();
			        app.setChannelToShow(newChannel);
			    }
			}
		});
	
	}
	
}
